package com.zheng.flink.study.dataset.datasource;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.util.Collector;

/**
 * @author zhengbo
 * @date 2019/12/11
 */
public class TextFileDataSource {

    /**
     * text文件对象
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class TextFileObj {

        private String name;

        private Integer amount;
    }

    public static void main(String[] args) throws Exception {

        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        env.readTextFile("file:///D:/etc/flink/dataSetText_test1.txt").flatMap(
                new FlatMapFunction<String, TextFileObj>() {

                    @Override
                    public void flatMap(String value, Collector<TextFileObj> out) throws Exception {
                        for (String str : value.split("\n")) {

                            String[] split = str.split(" ");

                            TextFileObj fileObj = new TextFileObj(split[0], Integer.parseInt(split[1]));
                            out.collect(fileObj);
                        }
                    }
                }
        ).print();
    }
}
